package com.rdb.mbean;


import com.rdb.entities.*;
import com.rdb.mbean.util.JsfUtil;
import com.rdb.mbean.util.PaginationHelper;
import com.rdb.sbean.MessagesFacade;
import com.rdb.sbean.RegistersFacade;
import com.rdb.sbean.StudentExamsFacade;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean(name = "customerController")
@SessionScoped
public class CustomerController implements Serializable {

    private Registers current;
    private String enRoll="";
    private Date dateNow = new Date();
    private DataModel items = null;
    @EJB
    private com.rdb.sbean.RegistersFacade ejbFacade;
    
    @EJB
    private com.rdb.sbean.StudentExamsFacade ejbExamsFacade;
    
    @EJB
    private com.rdb.sbean.MessagesFacade ejbMessagesFacade;
    
    
    private StudentExams studentExamsSelected;

    public CustomerController() {
    }

    public StudentExams getStudentExamsSelected() {
        return studentExamsSelected;
    }

    public MessagesFacade getEjbMessagesFacade() {
        return ejbMessagesFacade;
    }

    public void setEjbMessagesFacade(MessagesFacade ejbMessagesFacade) {
        this.ejbMessagesFacade = ejbMessagesFacade;
    }

    
    public void setStudentExamsSelected(StudentExams studentExamsSelected) {
        this.studentExamsSelected = studentExamsSelected;
    }

    public StudentExamsFacade getEjbExamsFacade() {
        return ejbExamsFacade;
    }

    public void setEjbExamsFacade(StudentExamsFacade ejbExamsFacade) {
        this.ejbExamsFacade = ejbExamsFacade;
    }

    public String getEnRoll() {
        return enRoll;
    }

    public void setEnRoll(String enRoll) {
        this.enRoll = enRoll;
    }
    
    public List<StudentExams> getSchedule(){
        List<StudentExams> listExamses = new ArrayList<StudentExams>();
        if(!"".equals(enRoll)){
            listExamses = ejbExamsFacade.getSchedule(enRoll);
        }
        return listExamses;
    }
    
    public List<StudentExams> getMarks(){
        List<StudentExams> listExamses = new ArrayList<StudentExams>();
        if(!"".equals(enRoll)){
            listExamses = ejbExamsFacade.getMarks(enRoll);
        }
        return listExamses;
    }
    
    private RegistersFacade getFacade() {
        return ejbFacade;
    }

    

    public String prepareList() {
        return "List";
    }

    
    public String prepareCreate() {
        current = new Registers();
        current.setId(studentExamsSelected);
        current.setStatus(Boolean.FALSE);
        current.setCreateDate(new Date());
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("bundle/Bundle").getString("RegistersCreated"));
            return "List";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("bundle/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
        
    }

    public String create() {
        try {
            getFacade().create(current);
            
            Messages messages = new Messages();
            messages.setExamId(null);
            messages.setStatus(Boolean.FALSE);
            messages.setRegisterId(getFacade().findAll().get(getFacade().findAll().size() -1));
            messages.setUserId(current.getAssignFac());
            messages.setMessages("Student " +  current.getId().getStudentId().getEnRoll() + " register recouting. ");
            ejbMessagesFacade.create(messages);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("bundle/Bundle").getString("RegistersCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("bundle/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }
    
    public boolean checkRecounting(StudentExams se){
        Date d = new Date();
        if(se.getExamId().getDateReCounting().compareTo(d)>=0){
            for (int i = 0; i < ejbFacade.findAll().size(); i++) {
                if(ejbFacade.findAll().get(i).getId().getId() == se.getId()){
                    return false;
                }
            }
            return true;
        }else{
            return false;
        }
    }


    @FacesConverter(forClass = Registers.class)
    public static class CustomerControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            CustomerController controller = (CustomerController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "customerController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Registers) {
                Registers o = (Registers) object;
                return getStringKey(o.getReId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Registers.class.getName());
            }
        }
    }
    
}
